﻿using System;
using System.Collections.Generic;
using System.IO;
using Duo1JHttpServer.Framework.Config;
using Duo1JHttpServer.Framework.Enum;
using Duo1JHttpServer.Framework.Model;
using Duo1JHttpServer.Framework.Utils;

namespace Duo1JHttpServer.Framework.Analyzer
{
    /// <summary>
    /// 配置文件解析
    /// </summary>
    internal sealed class ConfigAnalyzer
    {
        /// <summary>
        /// 解析配置文件
        /// </summary>
        public static void AnalyzeConfig()
        {
            string configRoot = GlobalDefine.StaticRoot;
            ConsoleUtils.WriteYellowLine($"ConfigFolder: {configRoot}");

            //读取所有Xml配置文件路径到缓存
            List<string> configPathCache = new List<string>();
            try
            {
                if (Directory.Exists(configRoot))
                {
                    DirectoryInfo directoryInfo = new DirectoryInfo(configRoot);
                    LoadConfigPath(directoryInfo, configPathCache);
                }
                else
                {
                    ConsoleUtils.WriteRedLine($"Don`t exist folder: {configRoot}");
                    return;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            DispatchXmlConfig(configPathCache);
        }

        /// <summary>
        /// 递归加载所有配置文件路径到缓存
        /// </summary>
        /// <param name="directoryInfo">配置文件根文件夹</param>
        /// <param name="pathCache">路径缓存List</param>
        private static void LoadConfigPath(DirectoryInfo directoryInfo, List<string> pathCache)
        {
            FileInfo[] fileInfos = directoryInfo.GetFiles();
            foreach (FileInfo fileInfo in fileInfos)
            {
                if (GlobalDefine.XmlRegex.IsMatch(fileInfo.Name))
                {
                    ConsoleUtils.WriteTabLine($"LoadXmlFile: {fileInfo.FullName}");
                    pathCache.Add(fileInfo.FullName);
                }
            }

            DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
            foreach (DirectoryInfo dicInfo in directoryInfos)
            {
                LoadConfigPath(dicInfo, pathCache);
            }
        }

        /// <summary>
        /// 分发Xml配置到对应解析方法
        /// </summary>
        /// <param name="pathCache">路径缓存List</param>
        private static void DispatchXmlConfig(List<string> pathCache)
        {
            Dictionary<ConfigType, List<string>> pathTypeDic = new Dictionary<ConfigType, List<string>>();

            foreach (string path in pathCache)
            {
                foreach (AnalyzerModel model in GlobalDefine.AnalyzerModels)
                {
                    if (model.PathRegex.IsMatch(path))
                    {
                        if (!pathTypeDic.ContainsKey(model.Type))
                        {
                            pathTypeDic[model.Type] = new List<string>();
                        }

                        pathTypeDic[model.Type].Add(path);
                    }
                }
            }

            foreach (KeyValuePair<ConfigType, List<string>> kv in pathTypeDic)
            {
                foreach (AnalyzerModel model in GlobalDefine.AnalyzerModels)
                {
                    if (model.Type == kv.Key)
                    {
                        model.Analyzer.Analyze(kv.Value);
                        break;
                    }
                }
            }
        }
    }
}